﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace SQL_Datebase_inset_delete_reader_update
{
    public class CourseService
    {
        /// <summary>
        ///这个方法使用多个参数添加课程，非常麻烦
        /// </summary>
        /// <param name="coursName"></param>
        /// <param name="courseContent"></param>
        /// <param name="classHour"></param>
        /// <param name="credit"></param>
        /// <param name="categoryId"></param>
        /// <param name="teacherId"></param>
        /// <returns></returns>
        //增
        public int AddCourse(string coursName, string courseContent, int classHour, int credit, int categoryId, int teacherId)//这里参数太多不方便，所以我们用实例类来解决
        {
            //定义sql语句
            string sql = " insert into Course(CourseName, CourseContent, ClassHour, Credit, CategoryId, TeacherId)";
            sql += $"values ('{coursName}','{courseContent}','{classHour}','{credit}','{categoryId}','{teacherId}')";
            //执行sql语句
            return SQLHelper.Update(sql);
        }
    
   /// <summary>
   /// 添加课程（通过实体类作为参数）
   /// </summary>
   /// <param name="course">课程对象</param>
   /// <returns></returns>

        //增（使用对象）重载
        public int AddCourse(Course course)//这里参数太多不方便，所以我们用实例类来解决
        {
            //定义sql语句，并解析实体数据
            string sql = " insert into Course(CourseName, CourseContent, ClassHour, Credit, CategoryId, TeacherId)";
            sql += $"values ('{course.CourseName}','{course.CourseContent}','{course.ClassHour}','{course.Credit}','{course.CategoryId}','{course.TeacherId}')";
            return SQLHelper.Update(sql);
        }
        //改
        public int UpdateCourse(Course course)
        {
            string sql = $"update Course set CourseName={course.CourseName},CourseContent={course.CourseContent},ClassHour={course.ClassHour},Credit={course.Credit}";
            sql += $"CategoryId={course.CategoryId},TeacherId={course.TeacherId}";
            sql += $"where CourseId={course.CourseId}"; 
            return SQLHelper.Update(sql);
        }
        //删(使用参数）
        public int DeleteCourse(int courseId)
        {
            string sql = $"delete from Course where CourseId={courseId}";
            return SQLHelper.Update(sql);
        }
        //删(使用对象）重载
        public int DeleteCourse(Course course)
        {
            string sql = $"delete from Course where CourseId={course.CourseId}";
            return SQLHelper.Update(sql);
        }

    }

}
